require(['./config'], () => {
    require(['template', 'jquery'], (template) => {
        class Cart {
            constructor() {
                this.prodList = []
                this.init()
            }
            init() {
                const prodList = localStorage.getItem('prodList')
                if (prodList && prodList != '[]') {
                    this.prodList = JSON.parse(prodList)
                    this.render()
                } else {
                    alert('购物车暂无数据')
                    location.href = '/'
                }
            }
            render() {
                $('.shopp').html(template('shopcar', { list: this.prodList }))
                this.computedAll()
                this.delProd()
                this.addReduce()
                this.checkedAll()
            }
            computedAll() {
                let allcount = 0, checkedcount = 0, allprice = 0
                this.prodList.forEach(item => {
                    allcount += item.count
                    if (item.isChecked) {
                        checkedcount += item.count
                        allprice += item.count * item.price
                    }
                })
                $('.sum').text(allcount)
                $('.summ').text(checkedcount)
                $('.ze').text(allprice.toFixed(2))

            }
            //删除商品
            delProd() {
                const dele = document.querySelectorAll('.dele'),
                    that = this
                for (let i = 0; i < dele.length; i++) {
                    dele[i].onclick = function () {
                        const id = $(this).attr('prodid') - 0
                        // console.log(id);
                        for (let j = 0; j < that.prodList.length; j++) {
                            if (that.prodList[j].id === id) {
                                that.prodList.splice(j, 1)
                            }
                        }
                        localStorage.setItem('prodList', JSON.stringify(that.prodList))
                        that.init()
                    }
                }
            }
            //数量加减
            addReduce() {
                const that = this
                $('.jian').click(function () {
                    const id = $(this).attr('prodid') - 0
                    that.prodList.forEach(item => {
                        if (item.id === id) {
                            item.count--
                            if (item.count <= 1) {
                                item.count = 1
                            }
                        }
                    })
                    // console.log(that.prodList)
                    localStorage.setItem('prodList', JSON.stringify(that.prodList))
                    that.init()
                })

                $('.jia').click(function () {
                    const id = $(this).attr('prodid') - 0
                    that.prodList.forEach(item => {
                        if (item.id === id) {
                            item.count++
                        }
                    })
                    localStorage.setItem('prodList', JSON.stringify(that.prodList))
                    that.init()
                })
            }
            //全选
            checkedAll() {
                $('.checkall').change(function () {
                    $('.check').prop('checked', $(this).prop('checked'))
                    if ($(this).prop('checked')) {
                        $('.check').parents('li').css('backgroundColor', '#FFFAED')
                    } else { $('.check').parents('li').css('backgroundColor', '#fff') }
                })
                $('.check').change(function () {
                    if ($('.check:checked').length === $('.check').length) {
                        $('.checkall').prop('checked', true)
                    } else {
                        $('.checkall').prop('checked', false)
                    }
                    if ($(this).prop('checked')) {
                        $(this).parents('li').css('backgroundColor', '#FFFAED')
                    } else { $(this).parents('li').css('backgroundColor', '#fff') }
                })
            }
        }
        return new Cart()
    })
})
// getSum()
// getMa()
// $('.jia').click(function(){
//     let n = $(this).prev().text()
//     n++
//     $(this).prev().text(n)
//    const z = $(this).parent().parent().next().find('.ma').text()
//    $(this).parent().parent().next().next().find('.mar').text(z*n) 
//    getSum()
//    getMa()
// })
// $('.jian').click(function(){
//     let n = $(this).next().text()
//     if(n ==1){
//         return false
//     }
//     n--
//     $(this).siblings('.number').text(n)
//     const z = $(this).parents('td').next().find('.ma').text()
//    $(this).parents('td').next().next().find('.mar').text(z*n)
//    getSum() 
//    getMa()
// })


// 





